<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * get record
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 153 $ / $LastChangedDate: 2010-02-18 17:21:41 +0100 (jeu., 18 févr. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Gospecies\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractJsonController;
use Zend\View\Model\JsonModel;

class AjaxRecordGetJsonController extends AbstractIdentityJsonController
{
    /**
     */
    public function indexAction()
    {
        // get logged user
        $this->user = $this->get_identity();

        $params = array();

        if ($this->request->getPost()->id_record !== null) {
            $params['id_record'] = $this->request->getPost()->id_record;
        }

        $params['geometry_column'] = 'point';
        $params['default_display'] = false;

        try {

              $RecordGet = $this->CoreModel('RecordGet');

              $record  = $RecordGet->run( $params );

              if ($record instanceof \Core\Library\Exception) {
                  return new JsonModel(array('error' => $record->getMessage()));

              }

              // check permission if id_group is "contributor" or above.
              // users of those groups can only update own records
              //
              if ((isset($this->user->id_group) && ((int)$this->user->id_status == 200))) {
                  if ((int)$this->user->id_group >= 300) {
                      if ($this->user->id_user !== $record['id_owner']) {
                          return new JsonModel(array('error' => 'permission denied'));
                      }
                  }
              } else {
                  return new JsonModel(array('error' => 'permission denied'));
              }

              $RecordMediaGetAll = $this->CoreModel('RecordMediaGetAll');

              $params  = array('id_record' => $params['id_record'],
                               'id_type'   => 1);

              $media = $RecordMediaGetAll->run( $params );

              if ($media instanceof \Core\Library\Exception) {

                return $this->error( $media->getMessage(), __file__, __line__);

              } else {

                 if (isset($media[0])) {

                   foreach($media as &$m) {

                        $_tmp = json_decode($m['media']);

                        $_tmp->media_folder_hash = $record['media_folder_hash'];

                      $m['media'] = $_tmp;
                   }

                 }

                 $record['image'] = $media;

              }

              return new JsonModel($record);

          } catch (\Exception $e) {

            $this->default_error = "Error\n File: " . __file__ . "\nLine: " . __line__ . "\nError: " . $e->getTraceAsString();

            return new JsonModel(array('error' => $this->default_error));

          }

    }
}

